import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/apartment/api.dart";
import "package:ct_pa_majordomo/pages/bill/accounting/index.dart";
import "package:ct_pa_majordomo/pages/bill/collected/index.dart";
import "package:ct_pa_majordomo/pages/bill/index.dart";
import "package:ct_pa_majordomo/pages/home/api.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/index.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/divider/solid_divider.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";

class BillManage extends StatefulWidget {
  const BillManage({super.key});

  @override
  State<StatefulWidget> createState() => _BillManageState();

}

class _BillManageState extends State<BillManage> {

  Map _billStatistics = {};

  void _getBillStatistics() async {
    Map resData = await homeApi.billStatistics();
    if (context.mounted) {
      setState(() {
        _billStatistics = resData["data"] ?? {};
      });
    }
  }

  String _formatCurrency(dynamic data) {
    return Utils.formatCurrency(Utils.toDouble(data));
  }

  @override
  void initState() {
    super.initState();
    _getBillStatistics();
  }

  Widget _item({
    required String title,
    required String content,
    Color? titleColor
  }) {
    return Container(
      height: 60,
      padding: const EdgeInsets.symmetric(horizontal: ThemeVars.paddingPage, vertical: 6),
      decoration: const BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(5)),
          color: ThemeVars.colorPrimaryLight
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(title, style: TextStyle(fontSize: 12, color: titleColor ?? ThemeVars.colorTextSec),),
          Text(content, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500),),
        ],
      ),
    );
  }

  Widget _block({
    required String title,
    required String actionName,
    required Widget child,
    GestureTapCallback? onTap,
  }) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: onTap,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(title, style: const TextStyle(fontSize: 12, fontWeight: FontWeight.w500),),
                Row(
                  children: [
                    Text(actionName, style: const TextStyle(color: ThemeVars.colorPrimary, fontSize: 12),),
                    const Icon(Icons.arrow_forward_ios, color: ThemeVars.colorPrimary, size: 12,)
                  ],
                )
              ],
            ),
            const SizedBox(height: 12,),
            child,
          ],
        ),
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "账单管理",
      child: Stack(
        children: [
          SizedBox(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            child: ListView(
              padding: const EdgeInsets.fromLTRB(ThemeVars.paddingPage, ThemeVars.paddingPage, ThemeVars.paddingPage, 60),
              children: [
                _block(
                  title: "待收账单(30天)",
                  actionName: "待收账单",
                  onTap: () {
                    Stacker.pushNamed(Keys.BILL, arguments: {"current": 0, "params": {"status": "2", "name": "逾期"} });
                  },
                  child: Row(
                    children: [
                      Expanded(child: _item(title: "${_billStatistics["pendingCount"] ?? 0}笔", content: "${_formatCurrency(_billStatistics["pendingAmount"]) ?? 0.00}元")),
                      const SizedBox(width: 16,),
                      Expanded(child: _item(title: "已逾期", content: "${_formatCurrency(_billStatistics["overdueAmount"]) ?? 0.00}元", titleColor: ThemeVars.colorWarning)),
                    ],
                  )
                ),
                const SizedBox(height: 12,),
                _block(
                    title: "本月收支",
                    actionName: "完成账单",
                    onTap: () {
                      Stacker.pushNamed(Keys.BILL, arguments: 1);
                    },
                    child: Row(
                      children: [
                        Expanded(child: _item(title: "收入", content: "${_formatCurrency(_billStatistics["monthlyIncome"]) ?? 0.00}元")),
                        const SizedBox(width: 16,),
                        Expanded(child: _item(title: "支出", content: "${_formatCurrency(_billStatistics["monthlyExpense"]) ?? 0.00}元",)),
                      ],
                    )
                ),
              ],
            ),
          ),
          Positioned(
            left: 0,
            bottom: 0,
            width: MediaQuery.of(context).size.width,
            height: 48,
            child: Container(
              width: MediaQuery.of(context).size.width,
              height: 48,
              padding: EdgeInsets.symmetric(vertical: ThemeVars.paddingPage),
              decoration: BoxDecoration(
                color: Colors.white
              ),
              child: Row(
                children: [
                  Expanded(
                    child: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        Stacker.pushNamed(Keys.BILL_COLLECTED);
                      },
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(Icons.note_add_rounded, color: ThemeVars.colorPrimary,),
                          Text("收款", style: TextStyle(color: ThemeVars.colorPrimary,),)
                        ],
                      ),
                    )
                  ),
                  SolidDivider(direction: Axis.vertical, color: ThemeVars.colorPrimary,),
                  Expanded(
                    child: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        Stacker.pushNamed(Keys.BILL_ACCOUNTING);
                      },
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(Icons.note_add_rounded, color: ThemeVars.colorPrimary,),
                          Text("记账", style: TextStyle(color: ThemeVars.colorPrimary,),)
                        ],
                      ),
                    )
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

}